package com.szqhyxj.youxiangju.biz.merchantHome.plan.particulars;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.szqhyxj.youxiangju.BaseActivity;
import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.merchantHome.MerchantContainerActivity;
import com.szqhyxj.youxiangju.biz.merchantHome.MerchantType;
import com.szqhyxj.youxiangju.biz.merchantHome.plan.adapter.PlanParticularsAdapter;
import com.szqhyxj.youxiangju.biz.plan.PlanDetail;
import com.szqhyxj.youxiangju.biz.plan.PlanModel;
import com.szqhyxj.youxiangju.biz.plan.PlanStatus;
import com.szqhyxj.youxiangju.biz.plan.RowsItem;
import com.szqhyxj.youxiangju.biz.plan.TaskType;
import com.szqhyxj.youxiangju.biz.plan.TasksItem;
import com.szqhyxj.youxiangju.sys.components.NavigationBar;
import com.szqhyxj.youxiangju.sys.components.view.popview.SingleImgDialog;
import com.szqhyxj.youxiangju.sys.util.StringUtils;

import java.util.List;
import java.util.Locale;
import java.util.Map;

import butterknife.BindColor;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PlanParticularsActivity extends BaseActivity implements NavigationBar.NavigationBarListener, PlanParticularsContract.View {

    private static final String PLANDETAIL = "planDetail";
    private static final String STARTTYPE = "startType";
    private static final String ROWSITEM = "rowsitem";
    @BindView(R.id.navigationBar)
    NavigationBar navigationBar;

    ExpandableListView detailList;
    @BindView(R.id.title_err)
    TextView titleErr;
    @BindView(R.id.shrink)
    LinearLayout shrink;

    @BindView(R.id.shrink_line)
    LinearLayout shrinkLine;
    @BindView(R.id.project_number)
    TextView projectNumber;
    @BindView(R.id.reimbursement_days)
    TextView reimbursementDays;
    @BindView(R.id.state_plan)
    TextView statePlan;
    @BindView(R.id.information_background)
    LinearLayout informationBackground;
    @BindView(R.id.repayment_amount)
    TextView repaymentAmount;
    @BindView(R.id.shuoming_zzj)
    ImageView shuomingZzj;
    @BindView(R.id.working_fund)
    TextView workingFund;
    @BindView(R.id.reimbursement_charge)
    TextView reimbursementCharge;
    @BindView(R.id.number)
    TextView number;

    @BindColor(R.color.colorTextBlue)
    int blue;
    @BindView(R.id.refund)
    LinearLayout refund;

    private String startType;
    private RowsItem rowsItem;
    private PlanDetail planDetail;

    public static void start(Activity activity, RowsItem rowsItem) {
        Intent intent = new Intent();
        intent.setClass(activity, PlanParticularsActivity.class);
        intent.putExtra(ROWSITEM, rowsItem);
        activity.startActivity(intent);
    }

    public static void start(Activity activity, PlanDetail planDetail, String startType) {
        Intent intent = new Intent();
        intent.setClass(activity, PlanParticularsActivity.class);
        intent.putExtra(PLANDETAIL, planDetail);
        intent.putExtra(STARTTYPE, startType);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_plan_particulars);
        detailList = findViewById(R.id.detail_list);
        initViews();
        ButterKnife.bind(this);
        navigationBar.setNavigationBarListener(this);
        initEvent();
        PlanParticularsPresenter presenter = new PlanParticularsPresenter(this);
        rowsItem = getIntent().getParcelableExtra(ROWSITEM);
        if (rowsItem != null) {
            String planId = rowsItem.getPlanId();
            presenter.init(planId);
        }
        planDetail = getIntent().getParcelableExtra(PLANDETAIL);
        if (planDetail != null) {
            String planId = planDetail.getPlanId();
            presenter.init(planId);
        }
        startType = getIntent().getStringExtra(STARTTYPE);
        //BindColor获取不到值，没查原因
        blue = getResources().getColor(R.color.colorTextBlue);
    }

    public void initViews() {
        View headerView = LayoutInflater.from(this).inflate(R.layout.plan_particulars_header_layout, null);
        detailList.addHeaderView(headerView);
        //隐藏
        View footerView = LayoutInflater.from(this).inflate(R.layout.plan_particulars_footer_layout, null);
        footerView.setVisibility(View.GONE);
        detailList.addFooterView(footerView);
    }

    public void initEvent() {
        detailList.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {
                if (expandableListView.isGroupExpanded(i)) {
                    expandableListView.collapseGroup(i);
                } else {
                    //第二个参数false表示展开时是否触发默认滚动动画
                    expandableListView.expandGroup(i, false);
                }
                //telling the listView we have handled the group click, and don't want the default actions.
                return true;
            }
        });
    }

    @Override
    public void navigationLeft() {
        if (startType != null && startType.equals(PlanModel.PRESENT)) {
            MerchantContainerActivity.start(this, false, MerchantType.PLAN.getCode());
        } else {
            finish();
        }
    }

    @Override
    public void navigationRight() {
    }

    @Override
    public void showParticulars(PlanDetail planDetail) {
        String cardNo = planDetail.getCardNo();
        String title = "";
        if (rowsItem != null) {
            title = rowsItem.getBankName();
        } else {
            title = planDetail.getBankName();
        }
        navigationBar.setNaviTitle(title);
        statePlan.setVisibility(View.VISIBLE);
        if (planDetail.getStatus() == PlanStatus.RUNNING.getCode()) {
            refund.setVisibility(View.GONE);
            statePlan.setText("执行中");
            statePlan.setTextColor(getResources().getColor(R.color.colorTextBlue));
        }
        if (planDetail.getStatus() == PlanStatus.PAUSED.getCode()) {
            refund.setVisibility(View.VISIBLE);
            shrinkLine.setVisibility(View.VISIBLE);
            titleErr.setText(rowsItem.getFailMsg());
            statePlan.setText("计划暂停");
            statePlan.setTextColor(getResources().getColor(R.color.textcolor4e));
        }
        if (planDetail.getStatus() == PlanStatus.FAIL.getCode()) {
            refund.setVisibility(View.VISIBLE);
            shrinkLine.setVisibility(View.VISIBLE);
            titleErr.setText(rowsItem.getFailMsg());
            statePlan.setText("异常结束");
            statePlan.setTextColor(getResources().getColor(R.color.textcolor4e));
        }
        if (planDetail.getStatus() == PlanStatus.SUCCESS.getCode()) {
            refund.setVisibility(View.GONE);
            statePlan.setText("正常完成");
            statePlan.setTextColor(blue);
        }
        projectNumber.setText(planDetail.getPlanId());
        reimbursementDays.setText(String.format(Locale.getDefault(), "%d天", planDetail.getPlanLength()));
        StringUtils.setColorTxt(this, repaymentAmount, blue, 16, String.format(Locale.getDefault(), "%.2f", planDetail.getTotalAmount()), blue, 12, " 元");
        StringUtils.setColorTxt(this, workingFund, blue, 16, String.format(Locale.getDefault(), "%.2f", planDetail.getWorkingAmount()), blue, 12, " 元");
        StringUtils.setColorTxt(this, reimbursementCharge, blue, 16, String.format(Locale.getDefault(), "%.2f", planDetail.getPayCostTotal()), blue, 12, " 元");
        int repayCount = 0;
        for (TasksItem tasksItem : planDetail.getTasks()) {
            if (tasksItem.getType() == TaskType.REPAY.getCode()) {
                repayCount++;
            }
        }
        StringUtils.setColorTxt(this, number, blue, 16, String.format(Locale.getDefault(), "%d", repayCount), blue, 12, " 笔");
    }

    @Override
    public void showList(List<List<Map<String, TasksItem>>> tasksList, List<PlanModel.PlanParticularsDayEntity> entityList) {
        if (tasksList.size() == 0 || entityList.size() == 0) {

        } else {
            PlanParticularsAdapter adapter = new PlanParticularsAdapter(tasksList, entityList);
            detailList.setAdapter(adapter);
        }

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (startType != null && startType.equals(PlanModel.PRESENT)) {
                MerchantContainerActivity.start(this, false, MerchantType.PLAN.getCode());
            } else {
                finish();
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @OnClick(R.id.shuoming_zzj)
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.shuoming_zzj:
                SingleImgDialog dialog = new SingleImgDialog(this);
                dialog.setImg(getResources().getDrawable(R.mipmap.zhouzhuanjin_bg));
                dialog.show();
                break;
        }

    }
}
